package test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DWCategory {
	private Map category = new HashMap();
	//private List elementList = new ArrayList(); // List of ids
	//private List elementNames = new ArrayList(); // List of Display names
	//private List parentList = new ArrayList();  // List of Parent ids
	private List<DWObject> lDWObjs = new ArrayList<DWObject>();
	private int current =0;
	
	public void put (String[] categTree) {
		Map elem;
		String id="",prefix="";
		elem = category;

		for (int i=0,len=categTree.length;i<len;i++) {
			categTree[i]=categTree[i].trim();
			//id+=prefix+categTree[i]; prefix="-";
			if (elem.get(categTree[i])==null) {
				//elementList.add(id+prefix+categTree[i]);
				//elementNames.add(categTree[i]);
				//parentList.add(id);
				DWObject dwo = new DWObject("category");
				dwo.setAttribute("category-id", id+prefix+categTree[i]);
				dwo.addChildObject("display-name",categTree[i]);
				if(!"".equalsIgnoreCase(id)) dwo.addChildObject("parent",id);
				lDWObjs.add(dwo);
				
				elem.put(categTree[i], new HashMap());
				//System.out.println("Appended new Category:"+categTree[i]+" Depth in tree:"+i);
				System.out.println("Category id ---------------------------------------" +id+prefix+categTree[i]);
			}
			id+=prefix+categTree[i]; prefix="-";
			elem = (Map) elem.get(categTree[i]);
		}
		elem = null;
	}
	
	public boolean hasNext() {
		
		//return elementList.size()>current;
		return lDWObjs.size()>current;
	}
	public DWObject next() {
		//if(!(elementList.size()>current)) {return null;}
		//DWObject dwo = new DWObject("category");
		//elementList.get(index)
		//dwo.setAttribute("category-id", (String) elementList.get(current));
		//dwo.addChildObject("display-name",(String) elementNames.get(current));
		//current++;
		//return dwo;
		
		return (lDWObjs.size()>current)
				?	(DWObject) lDWObjs.get(current++)
				:	null;
		
	}

	public void reset() {
		current = 0;
		return;
	}

}
